package com.leaves.javamall.product.controller;

import java.util.Arrays;
import java.util.List;
import java.util.Map;

//import org.apache.shiro.authz.annotation.RequiresPermissions;
import com.leaves.javamall.product.entity.AttrEntity;
import com.leaves.javamall.product.service.AttrAttrgroupRelationService;
import com.leaves.javamall.product.service.AttrService;
import com.leaves.javamall.product.service.CategoryService;
import com.leaves.javamall.product.vo.AttrGroupRelationVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import com.leaves.javamall.product.entity.AttrGroupEntity;
import com.leaves.javamall.product.service.AttrGroupService;
import com.leaves.common.utils.PageUtils;
import com.leaves.common.utils.R;



/**
 * 属性分组
 *
 * @author leaves
 * @email lxhu.work@gmail.com
 * @date 2020-08-10 16:47:20
 */
@RestController
@RequestMapping("product/attrgroup")
public class AttrGroupController {
    @Autowired
    private AttrGroupService attrGroupService;

    @Autowired
    private CategoryService categoryService;
    @Autowired
    private AttrService attrService;
    @Autowired
    private AttrAttrgroupRelationService attrAttrgroupRelationService;

    /**
     * 列表
     */
    @RequestMapping("/list/{categoryId}")
    //@RequiresPermissions("product:attrgroup:list")
    public R list(@RequestParam Map<String, Object> params, @PathVariable("categoryId") Long categoryId){
        PageUtils page = attrGroupService.queryPage(params,categoryId);

        return R.ok().put("page", page);
    }

    @PostMapping("/attr/relation")
    public R addRelation(@RequestBody List<AttrGroupRelationVo> vos){
        attrAttrgroupRelationService.saveBatch(vos);
        return R.ok();
    }

    /**
     * 信息
     */
    @RequestMapping("/info/{attrGroupId}")
    //@RequiresPermissions("product:attrgroup:info")
    public R info(@PathVariable("attrGroupId") Long attrGroupId){
		AttrGroupEntity attrGroup = attrGroupService.getById(attrGroupId);
        Long catelogId = attrGroup.getCatelogId();
        attrGroup.setCatelogPath(categoryService.findCatelogPath(catelogId));
        return R.ok().put("attrGroup", attrGroup);
    }

    /**
     * 保存
     */
    @RequestMapping("/save")
    public R save(@RequestBody AttrGroupEntity attrGroup){
		attrGroupService.save(attrGroup);
        return R.ok();
    }

    //
    @RequestMapping("/{attrgroupId}/attr/relation")
    public R attrRlation(@PathVariable("attrgroupId") Long attrgroupId){
        List<AttrEntity> relationAttr = attrService.getRelationAttr(attrgroupId);
        return R.ok().put("data",relationAttr);
    }

    // attr/norelation
    @GetMapping("/{attrgroupId}/noattr/relation")
    public R attrNoRelation(@RequestParam Map<String, Object> params, @PathVariable("attrgroupId") Long attrgroupId){
//        List<AttrEntity> relationAttr = attrService.getRelationAttr(attrgroupId);
        PageUtils page = attrService.getNoRelationAttr(params,attrgroupId);
        return R.ok().put("page",page);
    }

    ///product/attrgroup/{catelogId}/withattr
    @RequestMapping("/{catelogId}/withattr")
    public R getAttrGroupWithAttrs(@PathVariable("catelogId") Long catId){
        //1. 查出当前路径下所有的属性分组
        List<AttrGroupRelationVo> vos = attrGroupService.getgetAttrGroupWithAttrsByCatelogId(catId);
        //2. 查出每个属性分组的所有属性
        return R.ok().put("data",vos);
    }


    /**
     * 修改
     */
    @RequestMapping("/update")
    public R update(@RequestBody AttrGroupEntity attrGroup){
		attrGroupService.updateById(attrGroup);

        return R.ok();
    }

    /**
     * 删除
     */
    @RequestMapping("/delete")
    public R delete(@RequestBody Long[] attrGroupIds){
		attrGroupService.removeByIds(Arrays.asList(attrGroupIds));

        return R.ok();
    }

    @RequestMapping("/attr/relation/delete")
    public R deleteRelation(@RequestBody AttrGroupRelationVo[] vos){
        attrService.deleteRealtion(vos);
        return  R.ok();
    }

}
